const router = require('koa-router')()
const user = require("../controller/user.js")
const {sign} = require("jsonwebtoken")
const sequelize = require("../mysqlconnect.js")
const {QueryTypes} = require("sequelize")
router.prefix('/users')

router.get('/', function (ctx, next) {
  ctx.body = 'this is a users response!'
})

router.get('/bar', function (ctx, next) {
  ctx.body = 'this is a users/bar response'
})
router.get("/query",user.query)
router.get("/queryMember",async (ctx,next)=>{
	let user = ctx.state.user
	const res = await sequelize.query("select * from member where userId='"+user.id+"'",{type:QueryTypes.SELECT})
	ctx.body={code:200,data:res}
})
router.post("/login",async (ctx,next)=>{
	const params = ctx.request.body
	const isExist = await sequelize.query("select id,password from user where name='"+params.username+"'");
	// console.log(isExist,isExist.length)
	if(isExist.length){
		// console.log(params.password,isExist[0][0].password)
		if(params.password===isExist[0][0].password){
			const token = await sign({name:params.username,id:isExist[0][0].id},"secret618",{expiresIn:"3h"})
			// console.log(token)
			 ctx.body = {code:200,data:token}
		}else{
			ctx.body={code:500,msg:"用户名或密码错误"}
		}
	}else{
		ctx.body={code:500,msg:"用户名或密码错误"}
	}
})
router.post("/addAddress",async (ctx,next)=>{
	const {consignee,mobile,province_name,city_name,area_name,addr_detail,is_default,createTime,updateTime} = ctx.request.body
	const {id} = ctx.state.user
	if(Number(is_default)){
		await sequelize.query("update address set is_default=0 where is_default=1")
	}
	const num = await sequelize.query("insert into address (consignee,mobile,province_name,city_name,area_name,addr_detail,is_default,createTime,updateTime,userId)values (?,?,?,?,?,?,?,?,?,?)",{replacements:[
		consignee,mobile,province_name,city_name,area_name,addr_detail,Number(is_default),createTime,updateTime,id
	]})
	ctx.body = {code:200,msg:'操作成功'}
})
router.post("/updateAddress",async (ctx,next)=>{
	const {id,consignee,mobile,province_name,city_name,area_name,addr_detail,is_default,createTime,updateTime} = ctx.request.body
	const {userId} = ctx.state.user
	const sql = `update address set ${consignee?"consignee='"+consignee+"',":''}${mobile?"mobile='"+mobile+"',":''}${province_name?"province_name='"+province_name+"',":''}
${city_name?"city_name='"+city_name+"',":''}${area_name?"area_name='"+area_name+"',":''}${addr_detail?"addr_detail='"+addr_detail+"'":""} where id=${id}`
await sequelize.query(sql)
	ctx.body = {code:200,msg:'操作成功'}
})
router.get("/setDefaultAddress",async (ctx,next)=>{
	const {id} = ctx.query
	const userId = ctx.state.user.id
	await sequelize.query("update address set is_default = 0 where is_default=1 and userId=?",{replacements:[userId]})
	await sequelize.query("update address set is_default = 1 where id=? and userId=?",{replacements:[id,userId]})
	ctx.body = {code:200,msg:'操作成功'}
})
router.get("/delAddress",async (ctx,next)=>{
	const {id} = ctx.query
	await sequelize.query("delete from address where id=?",{replacements:[id]})
	ctx.body = {code:200,msg:'操作成功'}
})
router.get("/queryAddress",async (ctx,next)=>{
	const res = await sequelize.query("select * from address",{type:QueryTypes.SELECT})
	ctx.body = {code:200,data:res}
})
module.exports = router
